package com.application.Activity;

import it.unipr.ce.dsg.s2p.sip.Address;

import java.util.ArrayList;

import ReadWriteXML.CMember;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.NotificationManager;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RatingBar;

import com.application.Controller.ActivityController;
import com.application.Controller.NetworkController;
import com.application.Controller.ReadWriteFileController;
import com.application.Custom.ListWorkRatingAdapter;
import com.application.Custom.WorkRating;
import com.application.Network.NetworkLayer;
import com.application.Network.Protocol;
import com.application.PhanQuyen.Quyen;
import com.application.outdoor.InvoikeServices.OperationsInvoike;

public class frmDanhGiaActivity extends Activity implements RatingBar.OnRatingBarChangeListener{
	ArrayList<WorkRating> array;
	ListWorkRatingAdapter arrayAdapter;
	public static String userSender;
	Address sender;
	int port = Protocol.PORT;
	public static NetworkLayer _peer = null;
	public static Handler handler = new Handler();
		
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.layout_danh_gia);
		Log.d("PEERDROID","Da vao frm_Danh_Gia");
		
		//Lay ID & tat Notification
		NotificationManager nm=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
		Bundle ex = this.getIntent().getExtras();
		final int id = ex.getInt("ID");
		if(NetworkController.isInDoor()){
			sender = new Address(ex.getString("SENDER"));
			Log.d("PEERDROID","Da nhan duoc address Sender: "+ sender.toString());
		
		}
		nm.cancel(id);
		//Khoi tao ArrayList & ListWorkAdapter
        array = new ArrayList<WorkRating>();
        arrayAdapter = new ListWorkRatingAdapter(this, R.layout.list_rating, array);
        
        //Tao listView cho danh sach Danh gia
         ListView list = (ListView)findViewById(R.id.listView);
        list.setAdapter(arrayAdapter);
        
        
        ArrayList<CMember> cmem = ReadWriteFileController.readMember(ReadWriteFileController.MEMBER_PATH);
        for(int i=0; i<cmem.size(); i++){
        	Log.d("PEERDROID", String.valueOf(cmem.get(i).get_userName()));
        	WorkRating work = new WorkRating(cmem.get(i).get_userName());
        	array.add(work);
        }
      //  arrayAdapter.notifyDataSetChanged();
        
        //Khai bao & bat su kien Button send Result Danh Gia
        Button btnOK = (Button)findViewById(R.id.btnOK);
        btnOK.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				Quyen quyen = NetworkController._peer.getAccount().getRuleById(Protocol.R_NHAN_DANH_GIA);
				Log.d("PEERDROID","Quyen send = " + quyen.getSend());
				if (quyen != null && quyen.getSend()) {
					Log.d("PEERDROID", String.valueOf(array.get(0).getRating()));
					String result= "";
					
					for(int i=0; i<array.size(); i++){
						int rating=Math.round(array.get(i).getRating()*2);//Integer.parseInt((array.get(i).getRating()*2)+"");
						Log.v("Phu","raTIng="+rating);
						result +=  array.get(i).getTxtusername() + "/" + rating + ";";
					}
					Log.d("PEERDROID","xem result khi click OK "+result);
					if(NetworkController.isInDoor()){
						NetworkController.sendResultDanhGia(sender, result);
					}
					else if(NetworkController.isOutDoor()){
						//gui outdoor
						try{
							new OperationsInvoike().SendDanhGiaFeedback(CommonUtilities.nguoiDung.getTenDangNhap(), userSender,result);
							
						}catch (Exception e) {
							Log.d("Phu","Không thể gửi phản hồi đánh giá.");
						}
					}
					
					
					//ghi xuống file Log
					String text = NetworkController._peer.getAccount().get_Username()+" thực hiện đánh giá. ";
					ReadWriteFileController.checkRuleWriteToLogFile(text);
					ArrayList<Activity> arrActivity=ActivityController.arrActivity;
					for(int i=0;i<arrActivity.size();i++){
						Activity ac=arrActivity.get(i);
						if(ac.getLocalClassName().equals("frmDanhGiaActivity")){
							ActivityController.closeActivity(ac);
						}
					}
			        finish();
					
					//frmDanhGiaActivity.this.finish();
				}else{
					Show(Protocol.NON_RULE_MESSAGE);
					finish();
				}	
			}
		});
	}
	
	void closeWindow(){
		frmDanhGiaActivity.this.finish();
	}
	public void Show(String message) {
		
		AlertDialog dlg = new AlertDialog.Builder(this).setTitle("Thông báo").setMessage(message)
				.setPositiveButton("OK", new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
					}
				}).create();
		dlg.show();
	}
	@Override
	public void onRatingChanged(RatingBar ratingBar, float rating,
			boolean fromUser) {
		// TODO Auto-generated method stub
	}
	@Override
	public void onStart(){
		super.onStart();
		ActivityController.setPeerActivity(this);
		ActivityController.addActivityToArray(this);
	}
	@Override
	public void onResume(){
		super.onResume();
		ActivityController.setPeerActivity(this);
	}
	@Override
	public void onPause(){
		super.onPause();
		ActivityController.setPeerActivity(this);
	}
	

}
